package com.toocms.jinjuhang.ui.mine.bank_card

import androidx.databinding.ObservableField
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.StringUtils
import com.toocms.jinjuhang.R
import com.toocms.jinjuhang.config.Urls
import com.toocms.jinjuhang.config.UserRepository
import com.toocms.jinjuhang.data.center.BankItemData
import com.toocms.jinjuhang.dialog.hint.HintDialog
import com.toocms.jinjuhang.widget.view_model.requestData
import com.toocms.jinjuhang.widget.view_model.showToast
import com.toocms.tab.base.BaseActivity
import com.toocms.tab.base.ItemViewModel
import com.toocms.tab.binding.command.BindingAction
import com.toocms.tab.binding.command.BindingCommand

class BankCardItemModel(viewModel: BankCardListModel, val item: ObservableField<BankItemData>) :
    ItemViewModel<BankCardListModel>(viewModel) {

    val longClickBindingCommand = BindingCommand<BindingAction>(BindingAction {
        //TODO 删除银行卡
        HintDialog()
            .apply {
                content = StringUtils.getString(R.string.str_unbind_bank_card_hint)
                onPositiveClickListener = {
                    item.get()?.apply {
                        val params = mutableMapOf<String, String?>()
                        params["member_id"] = UserRepository.user.member_id
                        params["bank_id"] = bank_id
                        requestData(
                            Urls.Center.DEL_BANK,
                            params,
                            resultClass = String::class.java,
                            result = {
                                showToast(it)
                                viewModel.items -= this@BankCardItemModel
                            })
                    }
                }
                show((ActivityUtils.getTopActivity() as BaseActivity).supportFragmentManager, null)
            }
    })
}